# 3.7 render method

# 1. render(String view)

The render(String view) method will render the template pointed to by the view parameter. The rule for the final template file pointed to by the view is:

String template = baseViewPath + viewPath + view
1

In this formula:

  • view is the parameter carried by the render(String view) method.
  • baseViewPath and viewPath are two values specified during route configuration.

For example:

public void configRoute(Routes me) {
   // The baseViewPath is "/_view". This value will be taken by all Controllers mapped under this Routes object.
   me.setBaseViewPath("/_view");
   
   // basePath is the third parameter "/index".
   me.add("/", IndexController.class, "/index");
   
   // If the third parameter is omitted, basePath takes the value of the first parameter: "/project".
   me.add("/project", ProjectController.class);
}
1
2
3
4
5
6
7
8
9
10

For the above configuration, when using render in IndexController, the template file will be:

public class IndexController extends Controller {
   public void demo() {
     // The template points to: "/_view/index/abc.html".
     render("abc.html");
   }
}
1
2
3
4
5
6

The above render("abc.html") will point to the template located under the webapp directory at "/_view/index/abc.html".

# 2. Configuring the template engine for render(String view)

The render(String view) method will render the template file based on the me.setViewType(ViewType) method configured in configConstant(Constants me). For instance:

public void configConstant(Constants me) {
  me.setViewType(ViewType.JFINAL_TEMPLATE);
}
1
2
3

This configuration will use JFinal's built-in enjoy template engine to render the template. Note that this configuration only affects the Controller.render(String view) method; other render methods remain unchanged.

# 3. Other methods in the render series

The render series methods will render different types of views and return them to the client. JFinal currently supports views such as: JFinal Template, FreeMarker, JSP, Velocity, JSON, File, Text, Html, and QrCode. Besides the views supported by JFinal, you can also infinitely extend the view types by inheriting the Render abstract class.

Here are some usage examples:

// Render the template named "test.html" with the JFinal Template view type.
renderTemplate("test.html");
 
// Generate a QR code.
renderQrCode("content");
 
// Render the template named "test.html" with the FreeMarker view type.
renderFreeMarker("test.html");
 
// ... (other examples)
1
2
3
4
5
6
7
8
9
10

# 4. Customizing the implementation class of Controller.render series methods

JFinal provides the RenderFactory to customize the implementation class of all render methods in the Controller. Here is the code to customize the Controller.render(String view) implementation class:

// Customize a MyRender.
public class MyRender extends Render {
  ...
}
 
// Extend RenderFactory to switch Controller.render(String view)
// to our custom MyRender.
public class MyRenderFactory extends RenderFactory {
   public Render getRender(String view) {
      return new MyRender(view);
   }
}
 
// Configure to take effect.
public void configConstant(Constants me) {
   me.setRenderFactory(new MyRenderFactory());
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

In the above code, the MyRenderFactory.getRender(...) method overrides the parent class RenderFactory.getRender(...) method, switching the implementation class of Controller.render(String view). Similarly, the implementation class of all render methods in the Controller can be switched to custom classes in this way, providing great flexibility.

Last Updated: 9/17/2023, 6:05:19 AM